/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.DocumentKindEnum;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.config.dto.ConfigFastProcedureDto;
import com.icinfo.cloud.provider.punish.config.enums.DocumentTemplateTypeEnum;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigFastProcedureMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamDocumentInfo;
import com.icinfo.cloud.provider.punish.config.model.ConfigFastProcedure;
import com.icinfo.cloud.provider.punish.config.service.IConfigBusiParamDocumentInfoService;
import com.icinfo.cloud.provider.punish.config.service.IConfigFastProcedureService;
import com.icinfo.cloud.provider.punish.config.utils.AuditDataUtil;
import com.icinfo.cloud.provider.punish.config.vo.ClaimOrgVo;
import com.icinfo.cloud.provider.punish.config.vo.ConfigFastProcedureDocumentInfoVo;
import com.icinfo.cloud.provider.punish.config.vo.ConfigFastProcedureVo;
import com.icinfo.cloud.provider.punish.config.vo.PunishmentMattersVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述: 速结程序配置表 config_fast_procedure 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月05日
 */
@Service
public class ConfigFastProcedureServiceImpl extends MyBatisServiceSupport implements IConfigFastProcedureService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigFastProcedureServiceImpl.class);

    @Autowired
    private ConfigFastProcedureMapper configFastProcedureMapper;

    @Resource
    private IConfigBusiParamDocumentInfoService configBusiParamDocumentInfoService;
    @Resource
    private ICaseMainInfoService caseMainInfoService;
    /**
     * 获取速结程序列表
     * @param dto
     * @return
     */
    @Override
    public List<ConfigFastProcedureVo> fastProcedureList(ConfigFastProcedureDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getLength(), dto.getOrderBy());
        List<ConfigFastProcedureVo> list = configFastProcedureMapper.fastProcedureList(dto);
        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        list.stream().forEach(item->{
            if (StringUtils.isEmpty(item.getRecordTemplateCode()) && StringUtils.isEmpty(item.getSiteInspectionTemplateCode())){
                item.setDocumentTemplateType(DocumentTemplateTypeEnum.COMPLETE_CONFIGURATION_NO.getName());
            } else {
                item.setDocumentTemplateType(DocumentTemplateTypeEnum.COMPLETE_CONFIGURATION_YES.getName());
            }
        });
        return list;
    }

    /**
     * 新增速结程序
     * @param dto
     * @return
     */
    @Override
    public Integer saveFastProcedure(ConfigFastProcedureDto dto) {
        ConfigFastProcedure configFastProcedure = new ConfigFastProcedure();
        BeanUtils.copyProperties(dto, configFastProcedure);
        AuditDataUtil.fillAuditAddData(configFastProcedure);
        configFastProcedure.setId(com.icinfo.framework.tools.utils.StringUtils.uuid());
        return configFastProcedureMapper.insertSelective(configFastProcedure);
    }

    /**
     * 修改速结程序配置
     * @param dto
     * @return
     */
    @Override
    public Integer updateFastProcedure(ConfigFastProcedureDto dto) {
        ConfigFastProcedure configFastProcedure = new ConfigFastProcedure();
        BeanUtils.copyProperties(dto, configFastProcedure);
        AuditDataUtil.fillAuditDataForUpdate(configFastProcedure);
        Example example = new Example(ConfigFastProcedure.class);
        example.createCriteria().andEqualTo("id", dto.getId());
        return configFastProcedureMapper.updateByExampleSelective(configFastProcedure,example);
    }

    /**
     * 删除速结程序（逻辑删除）
     * @param dto
     * @return
     */
    @Override
    public Integer deleteFastProcedure(ConfigFastProcedureDto dto) {
        ConfigFastProcedure configFastProcedure = new ConfigFastProcedure();
        configFastProcedure.setIsDeleted("1");
        Example example = new Example(ConfigFastProcedure.class);
        example.createCriteria().andEqualTo("id", dto.getId());
        return configFastProcedureMapper.updateByExampleSelective(configFastProcedure,example);
    }

    /**
     * 获取速结程序详情
     * @param dto
     * @return
     */
    @Override
    public ConfigFastProcedureVo getFastProcedureDetail(ConfigFastProcedureDto dto) {
        ConfigFastProcedure configFastProcedure = configFastProcedureMapper.selectByPrimaryKey(dto.getId());
        ConfigFastProcedureVo configFastProcedureVo = new ConfigFastProcedureVo();
        BeanUtils.copyProperties(configFastProcedure,configFastProcedureVo);
        return configFastProcedureVo;
    }

    /**
     * 获取处罚事项
     * @param areaCode
     * @return
     */
    @Override
    public List<PunishmentMattersVo> getPunishmentMatters(String areaCode,String listName) {
        List<PunishmentMattersVo> list = new ArrayList<>();
        List<PunishmentMattersVo> punishmentMatters = configFastProcedureMapper.getPunishmentMatters(areaCode,listName);
        ConfigFastProcedureDto configFastProcedureDto = new ConfigFastProcedureDto();
        configFastProcedureDto.setAreaCode(areaCode);
        configFastProcedureDto.setListName(listName);
        List<ConfigFastProcedureVo> configFastProcedureVos = configFastProcedureMapper.fastProcedureList(configFastProcedureDto);
        if (!CollectionUtils.isEmpty(configFastProcedureVos)){
            for (PunishmentMattersVo punishmentMatter : punishmentMatters) {
                boolean flag = validation(punishmentMatter, configFastProcedureVos);
                if (!flag){
                    list.add(punishmentMatter);
                }
            }
        } else {
            return punishmentMatters;
        }
        return list;
    }

    /**
     * 获取认领部门信息
     * @param areaCode
     * @param orgName
     * @return
     */
    @Override
    public List<ClaimOrgVo> getClaimOrg(String areaCode, String orgName) {
        return configFastProcedureMapper.getClaimOrg(areaCode, orgName);
    }

    /**
     * 根据处罚事项code查询文书模板
     * @param orgCode
     * @param listCode
     * @return
     */
    @Override
    public ConfigFastProcedureVo getTemplateByListCode(String orgCode, String listCode) {
        return configFastProcedureMapper.getTemplateByListCode(orgCode, listCode);
    }

    /**
     * 根据部门code查询速结程序适用的处罚事项
     *
     * @author yangsizhen
     * @date 2022/12/28
     * @param orgCode 部门code
     * @return com.icinfo.framework.common.web.Result<com.icinfo.cloud.provider.punish.config.vo.ConfigFastProcedureVo>
     */
    @Override
    public List<ConfigFastProcedureVo> getListByOrgCode(String orgCode) {
        return configFastProcedureMapper.getListByOrgCode(orgCode);
    }

    /**
     * 根据listCode查询其个数
     * @param orgCode
     * @param listCode
     * @return
     */
    @Override
    public int getFastProcedureNumByListCodeAndOrgCode(String orgCode, String listCode){
        Example example = new Example(ConfigFastProcedure.class);
        example.createCriteria().andEqualTo("listCode", listCode)
                .andEqualTo("orgCode", orgCode)
                .andEqualTo("isDeleted", "0");
        return configFastProcedureMapper.selectCountByExample(example);
    }



    @Override
    public List<ConfigFastProcedureDocumentInfoVo> getDocumentTemplateByListCode(String orgCode, String listCode,String caseId) {
        CaseMainInfoVo caseMainInfo =caseMainInfoService.getCaseMainInfo(caseId);
        //  ConfigFastProcedureVo configFastProcedureVo = configFastProcedureMapper.getTemplateByListCode(orgCode, listCode);
        Example example = new Example(ConfigFastProcedure.class);
        example.createCriteria().andEqualTo("listCode", listCode)
                .andEqualTo("orgCode", orgCode)
                .andEqualTo("isDeleted", "0");
        List<ConfigFastProcedure> configFastProcedures = configFastProcedureMapper.selectByExample(example);
        List<ConfigFastProcedureDocumentInfoVo> documentInfoVoList = new ArrayList<>();
        if (ObjectUtil.isEmpty(configFastProcedures) || configFastProcedures.size()==0){
            return documentInfoVoList;
        }
        ConfigFastProcedure configFastProcedureVo=configFastProcedures.get(0);

        if (ObjectUtil.isNotEmpty(configFastProcedureVo)){
            List<DocumentKindEnum> documentKindCodeList = Arrays.asList(DocumentKindEnum.DOC_KIND_ZLGZTZS,
                    DocumentKindEnum.DOC_KIND_SDDZQRS,
                    DocumentKindEnum.DOC_KIND_DK2XCKYJCBL,
                    DocumentKindEnum.DOC_KIND_DK2XWBL,
                    DocumentKindEnum.DOC_KIND_DK2SFZM,
                    DocumentKindEnum.DOC_KIND_DK2KYBL,
                    DocumentKindEnum.DOC_KIND_DK2XCSYT,
                    DocumentKindEnum.DOC_KIND_DK2XCZP);
            //强制转换成list ,todo 待优化目前这种存储方式不利于解析使用
            for (DocumentKindEnum documentKindEnum : documentKindCodeList) {
                ConfigFastProcedureDocumentInfoVo documentInfoVo =new ConfigFastProcedureDocumentInfoVo();
                BeanUtils.copyProperties(configFastProcedureVo,documentInfoVo);
                ConfigBusiParamDocumentInfo configBusiParamDocumentInfo = configBusiParamDocumentInfoService.getFastCaseDocumentInfoByDocumentKindCode(caseMainInfo.getBusiParamId(), documentKindEnum.getCode(), Constant.STRING_1);
                if (ObjectUtil.isNotEmpty(configBusiParamDocumentInfo)){
                    documentInfoVo.setDocumentCatalogCode(configBusiParamDocumentInfo.getDocumentCatalogCode());
                    documentInfoVo.setDocumentCatalogName(configBusiParamDocumentInfo.getDocumentCatalogName());
                }
                documentInfoVo.setDocumentShowName(documentKindEnum.getDes()+"（模板）");
                documentInfoVo.setDocumentKindCode(documentKindEnum.getCode());
                documentInfoVo.setDocumentKindName(documentKindEnum.getDes());
                //送达地址确认书
                if ( documentKindEnum.getCode().equals(DocumentKindEnum.DOC_KIND_SDDZQRS.getCode())){
                    documentInfoVo.setDocumentShowName("送达地址确认书");
                    documentInfoVo.setDocumentTemplateCode("");
                    documentInfoVo.setIsRequired(Constant.STRING_1);
                }
                //送达地址确认书
                if ( documentKindEnum.getCode().equals(DocumentKindEnum.DOC_KIND_SDDZQRS.getCode())){
                    documentInfoVo.setDocumentShowName("责令改正通知书");
                    documentInfoVo.setDocumentTemplateCode("");
                    documentInfoVo.setIsRequired(Constant.STRING_1);
                }
                //现场检查模板
                if ( documentKindEnum.getCode().equals(DocumentKindEnum.DOC_KIND_DK2XCKYJCBL.getCode())){
                    documentInfoVo.setDocumentShowName("现场检查模板"+"（模板）");
                    documentInfoVo.setDocumentTemplateCode(configFastProcedureVo.getSiteInspectionTemplateCode());
                    documentInfoVo.setIsRequired(configFastProcedureVo.getSiteInspectionIsRequired());
                }
                //询问笔录
                if ( documentKindEnum.getCode().equals(DocumentKindEnum.DOC_KIND_DK2XWBL.getCode())){
                    documentInfoVo.setDocumentTemplateCode(configFastProcedureVo.getRecordTemplateCode());
                    documentInfoVo.setIsRequired(configFastProcedureVo.getRecordIsRequired());
                }
                //身份证明
                if ( documentKindEnum.getCode().equals(DocumentKindEnum.DOC_KIND_DK2SFZM.getCode())){
                    documentInfoVo.setDocumentTemplateCode(configFastProcedureVo.getSfzmTemplateCode());
                    documentInfoVo.setIsRequired(configFastProcedureVo.getSfzmIsRequired());
                }
                //现场示意图
                if ( documentKindEnum.getCode().equals(DocumentKindEnum.DOC_KIND_DK2XCSYT.getCode())){
                    documentInfoVo.setDocumentTemplateCode(configFastProcedureVo.getXcsytTemplateCode());
                    documentInfoVo.setIsRequired(configFastProcedureVo.getXcsytIsRequired());
                }
                if ( documentKindEnum.getCode().equals(DocumentKindEnum.DOC_KIND_DK2XCZP.getCode())){
                    documentInfoVo.setDocumentShowName("现场照片"+"（模板）");
                    documentInfoVo.setDocumentTemplateCode(configFastProcedureVo.getXczpjsmTemplateCode());
                    documentInfoVo.setIsRequired(configFastProcedureVo.getXczpjsmIsRequired());
                }
                //勘验笔录
                if ( documentKindEnum.getCode().equals(DocumentKindEnum.DOC_KIND_DK2KYBL.getCode())){
                    documentInfoVo.setDocumentShowName("勘验笔录"+"（模板）");
                    documentInfoVo.setDocumentTemplateCode(configFastProcedureVo.getKyblTemplateCode());
                    documentInfoVo.setIsRequired(configFastProcedureVo.getKyblIsRequired());
                }

                documentInfoVoList.add(documentInfoVo);
            }
        }
        return documentInfoVoList;
    }

    private boolean validation(PunishmentMattersVo punishmentMatter,List<ConfigFastProcedureVo> configFastProcedureVos){
        for (ConfigFastProcedureVo configFastProcedureVo : configFastProcedureVos) {
            if (configFastProcedureVo.getListCode().equals(punishmentMatter.getListCode())&&configFastProcedureVo.getOrgCode().equals(punishmentMatter.getOrgCode())){
                return true;
            }
        }
        return false;
    }


}
